
#include "Utils.h"
#include <stdio.h>
#include <algorithm>
#include <boost/algorithm/string.hpp>

#ifdef _WINDOWS
    #include <direct.h>
    #define GetCurrentDir _getcwd
	#define PathSeparator '\\'
	#define WindowsPathSepartor "\\"
	#define UnixPathSepartor "/"
#else
    #include <unistd.h>
    #define GetCurrentDir getcwd
	#define PathSeparator '/'
#endif

string ExtractDirectory(const string& path)
{
#ifdef _WINDOWS
	string pathStr = path;
	boost::replace_all(pathStr, UnixPathSepartor, WindowsPathSepartor);	 
	return pathStr.substr( 0, pathStr.find_last_of( PathSeparator) + 1 );
#else
	return path.substr( 0, path.find_last_of( PathSeparator) + 1 );
#endif
}

string ExtractFilename(const string& path)
{
#ifdef _WINDOWS
	string pathStr = path;
	boost::replace_all(pathStr, UnixPathSepartor, WindowsPathSepartor);	 
	return pathStr.substr( pathStr.find_last_of( PathSeparator ) + 1 );
#else
	return path.substr( path.find_last_of( PathSeparator ) + 1 );
#endif
}

string ChangeExtension(const string& path, const string& ext)
{
	string filename = ExtractFilename( path );
	return ExtractDirectory( path ) 
		+ filename.substr( 0, filename.find_last_of( '.' ) ) 
		+ ext;
}

string GetExecuteFilePath()
{
	char currentWorkDir[FILENAME_MAX] = {0};

	if (!GetCurrentDir(currentWorkDir, sizeof(currentWorkDir) / sizeof(char)))
	{
		return "";
	}

	currentWorkDir[sizeof(currentWorkDir) - 1] = '\0'; /* not really required */

	string s;
	s.assign(currentWorkDir);
	return s;
}

